package com.yt.controller;

import com.yt.biz.UserService;
import com.yt.entity.User;
import com.yt.util.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Classname UserController
 * @Description
 * @Date 2021/10/17 19:52
 * @Create by 杨涛
 */
@RestController
@Slf4j
public class UserController {

    @Resource
    UserService userService;
    @Value("${server.port}")
    private String serverport;

    @RequestMapping("/timeout")
    public String feigntimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverport;
    }

    @RequestMapping("/getinfo")
    public String getinfo(){
        return "provider - 8002-"+ UUID.randomUUID();
    }


    @RequestMapping("/getusers")
    public List<User> getUsers() {
        log.info("读用户表,serverport:"+serverport);
        return userService.getUser();
    }

    @RequestMapping("/getjsonusers")
    public CommonResult<List<User>> getJsonUsers() {
        log.info("读用户表");
        CommonResult<List<User>> cr = new CommonResult<>(
                200,
                "用户表所有用户,serverport:"+serverport,
                userService.getUser()
        );
        return cr;
    }


    @RequestMapping("/adduser")
    //如果consumer参数是对象，要使用
//    public CommonResult<User> addUser( @RequestBody String username, String password) {
    public CommonResult<User> addUser(String username, String password) {

        int line = userService.addUser(username, password);
        CommonResult<User> cr = null;
        if (line > 0) {
            cr = new CommonResult<>(200, "adduser ok serverport:"+serverport);
        }else{
            cr = new CommonResult<>(444,"数据插入失败,serverport:"+serverport);
        }
        log.info(cr.toString());
        return cr;
    }
}
